package org.thirdHero.db.service;

import com.github.pagehelper.PageHelper;
import org.thirdHero.db.dao.SysMenuMapper;
import org.thirdHero.db.domain.SysMenu;
import org.thirdHero.db.domain.SysMenuExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.util.List;

@Service
public class SysMenuService {
    private final SysMenu.Column[] result = new SysMenu.Column[]{SysMenu.Column.id, SysMenu.Column.code, SysMenu.Column.pcode, SysMenu.Column.pcodes, SysMenu.Column.name, SysMenu.Column.icon, SysMenu.Column.url,
                SysMenu.Column.num, SysMenu.Column.levels, SysMenu.Column.ismenu, SysMenu.Column.status};

    @Resource
    private SysMenuMapper sysMenuMapper;

    public List<SysMenu> querySelective(String menuName, Integer page, Integer limit, String sort, String order){
        SysMenuExample example = new SysMenuExample();
        SysMenuExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(menuName)) {
            criteria.andNameLike("%"+menuName+"%");
        }
        PageHelper.startPage(page,limit);
        return sysMenuMapper.selectByExampleSelective(example,result);
    }

    public int countSeletive(String name,Integer page,Integer size,String sort,String order){
        SysMenuExample example = new SysMenuExample();
        SysMenuExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(name)){
            criteria.andNameLike("%"+name+"%");
        }
        return (int) sysMenuMapper.countByExample(example);
    }
    public List<SysMenu> checkExistUrl(String url){
        SysMenuExample sysMenuExample = new SysMenuExample();
        SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();
        criteria.andUrlEqualTo(url);
        return sysMenuMapper.selectByExample(sysMenuExample);
    }
    public int inserLiteMallMenu(SysMenu sysMenu){
        return sysMenuMapper.insert(sysMenu);
    }
    public int deleteLitemallMenuById(Integer id){
        SysMenuExample sysMenuExample = new SysMenuExample();
        SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();
        criteria.andIdEqualTo(id);
        return sysMenuMapper.deleteByExample(sysMenuExample);
    }
    public int updateLiteMallById(SysMenu sysMenu){
        SysMenuExample sysMenuExample = new SysMenuExample();
        SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();
        criteria.andIdEqualTo(sysMenu.getId());
        return sysMenuMapper.updateByExample(sysMenu, sysMenuExample);
    }
    public List<SysMenu> queryLiteMallByName(String name){
        SysMenuExample sysMenuExample = new SysMenuExample();
        SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();
        criteria.andNameEqualTo(name);
        return sysMenuMapper.selectByExample(sysMenuExample);
    }
  /**
   * 根据code查询用户信息
   * @param code
   * @return
   */
  public List<SysMenu> findLiteMallMenuByCode(String code) {
        SysMenuExample sysMenuExample = new SysMenuExample();
        SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();
        criteria.andCodeEqualTo(code);
        return sysMenuMapper.selectByExample(sysMenuExample);
    }
    public List<SysMenu> findChildLitemallMenu(String code){
        SysMenuExample sysMenuExample = new SysMenuExample();
        SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();
        criteria.andPcodeEqualTo(code);
        return sysMenuMapper.selectByExample(sysMenuExample);
    }
    public SysMenu findLiteMallMenuById(Integer id){
        SysMenuExample sysMenuExample = new SysMenuExample();
        SysMenuExample.Criteria criteria = sysMenuExample.createCriteria();
        criteria.andIdEqualTo(id);
        return sysMenuMapper.selectOneByExample(sysMenuExample);
    }
}
